package com.ruoyi.coffee.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.coffee.domain.Categories;
import com.ruoyi.coffee.dto.QueryProductDto;
import com.ruoyi.coffee.mapper.CategoriesMapper;
import com.ruoyi.coffee.vo.PageResultVo;
import com.ruoyi.coffee.vo.ProductsVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.coffee.mapper.ProductsMapper;
import com.ruoyi.coffee.domain.Products;
import com.ruoyi.coffee.service.IProductsService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 咖啡产品Service业务层处理
 *
 * @author dxj
 * @date 2024-12-30
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {
    @Resource
    private ProductsMapper productsMapper;

    @Resource
    private CategoriesMapper categoriesMapper;

    /**
     * 查询咖啡产品
     *
     * @param productId 咖啡产品主键
     * @return 咖啡产品
     */
    @Override
    public Products selectProductsByProductId(Long productId) {
        return productsMapper.selectProductsByProductId(productId);
    }

    /**
     * 查询咖啡产品列表
     *
     * @param products 咖啡产品
     * @return 咖啡产品
     */
    @Override
    public List<Products> selectProductsList(Products products) {
        return productsMapper.selectProductsList(products);
    }

    /**
     * 新增咖啡产品
     *
     * @param products 咖啡产品
     * @return 结果
     */
    @Override
    public int insertProducts(Products products) {
        products.setCreateTime(DateUtils.getNowDate());
        return productsMapper.insert(products);
    }

    /**
     * 修改咖啡产品
     *
     * @param products 咖啡产品
     * @return 结果
     */
    @Override
    public int updateProducts(Products products) {
        products.setUpdateTime(DateUtils.getNowDate());
        return productsMapper.updateProducts(products);
    }

    /**
     * 批量删除咖啡产品
     *
     * @param productIds 需要删除的咖啡产品主键
     * @return 结果
     */
    @Override
    public int deleteProductsByProductIds(Long[] productIds) {
        return productsMapper.deleteProductsByProductIds(productIds);
    }

    /**
     * 删除咖啡产品信息
     *
     * @param productId 咖啡产品主键
     * @return 结果
     */
    @Override
    public int deleteProductsByProductId(Long productId) {
        return productsMapper.deleteProductsByProductId(productId);
    }

    /**
     * 五月榜单
     *
     * @return
     */
    @Override
    public List<ProductsVo> billboards() {
        // 创建分页对象，设置当前页码为1，每页大小为4
        Page<Products> page = new Page<>(1, 4);
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Products::getProductId, Products::getProductName, Products::getDescription, Products::getPrice, Products::getStockQuantity, Products::getCategoryId, Products::getImageUrl);
        // 执行分页查询
        Page<Products> userPage = this.page(page, queryWrapper);
        // 将结果转换为 ProductsVo 对象列表
        return convertProductsToVoList(userPage);
    }

    /**
     * 大师推荐
     *
     * @return
     */
    @Override
    public List<ProductsVo> recommend() {
        // 创建分页对象，设置当前页码为1，每页大小为4
        Page<Products> page = new Page<>(1, 2);
        LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Products::getProductId, Products::getProductName, Products::getDescription, Products::getPrice, Products::getStockQuantity, Products::getCategoryId, Products::getImageUrl).eq(Products::getCategoryId, 5);
        // 执行分页查询
        Page<Products> userPage = this.page(page, queryWrapper);
        // 将结果转换为 ProductsVo 对象列表
        return convertProductsToVoList(userPage);
    }

    /**
     * 根据条件搜索咖啡产品
     *
     * @param queryProductDto
     * @return
     */
    @Override
    public AjaxResult searchProductByCategoryId(QueryProductDto queryProductDto) {
        //分页对象
        IPage<Products> page = new Page<>(queryProductDto.getPageNum(), queryProductDto.getPageSize());
        //查询条件
        LambdaQueryWrapper<Products> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.select(Products::getProductId, Products::getProductName, Products::getDescription, Products::getPrice, Products::getStockQuantity, Products::getCategoryId, Products::getImageUrl);
        lambdaQuery.likeRight(StringUtils.hasText(queryProductDto.getProductName()), Products::getProductName, queryProductDto.getProductName());
        lambdaQuery.eq(queryProductDto.getCategoryId() != null, Products::getCategoryId, queryProductDto.getCategoryId());
        // 调用 selectPage 分页方法
        IPage<Products> productsIPage = baseMapper.selectPage(page, lambdaQuery);
        long total = productsIPage.getTotal();
        List<ProductsVo> productsVoList = convertProductsToVoList(productsIPage);
        PageResultVo<ProductsVo> resultVo = new PageResultVo<>();
        resultVo.setTotal(total);
        resultVo.setRecords(productsVoList);
        return AjaxResult.success(resultVo);
    }

    /**
     * 根据Id获取详情
     *
     * @param productId
     * @return
     */
    @Override
    public ProductsVo getDetailByProductId(Long productId) {
        return Optional.ofNullable(productsMapper.selectProductsByProductId(productId)).map(products -> {
            ProductsVo productsVo = new ProductsVo();
            BeanUtils.copyProperties(products, productsVo);
            //通过分类id 查询分类名称
            LambdaQueryWrapper<Categories> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(Categories::getCategoryName).eq(Categories::getCategoryId, products.getCategoryId());
            Categories categories = categoriesMapper.selectOne(wrapper);
            productsVo.setCategoryName(categories.getCategoryName());
            return productsVo;
        }).orElseGet(() -> {
            return null;
        });
    }

    /**
     * vo 对象转换
     *
     * @param userPage
     * @return
     */
    public List<ProductsVo> convertProductsToVoList(IPage<Products> userPage) {
        return userPage.getRecords().stream()
                .map(products -> {
                    ProductsVo productsVo = new ProductsVo();
                    BeanUtils.copyProperties(products, productsVo);
                    return productsVo; // 返回 ProductsVo 对象
                })
                .collect(Collectors.toList());
    }
}
